/**
 * 
 */
package org.swing.utility.system.zip;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

/**
 * @author lqnhu
 *
 */
public class ZipInputStream extends java.util.zip.ZipInputStream {
	public ZipInputStream(InputStream in) throws IOException {
		super(skipToZipStart(in));
	}

	public ZipInputStream(InputStream in, Charset charset) throws IOException {
		super(skipToZipStart(in), charset);
	}

	private static final int[] ZIP_HEADER = new int[] { 'P', 'K', 0x03, 0x04 };

	public static InputStream skipToZipStart(InputStream is) throws IOException {
		if (!is.markSupported())
			is = new BufferedInputStream(is);
		int state = 0;
		for (;;) {
			if (state == 0)
				is.mark(ZIP_HEADER.length);
			final int b = is.read();
			if (b < 0)
				throw new IllegalArgumentException("Not a JAR/ZIP file");
			if (state >= 0 && b == ZIP_HEADER[state]) {
				state++;
				if (state == ZIP_HEADER.length)
					break;
			} else {
				state = -1;
				if (b == '\n' || b == 0) // start matching on \n and \0
					state = 0;
			}
		}
		is.reset();
		return is;
	}
}
